package com.jbh.wanandroidkotlin.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.fragment.IndexFragment
import com.jbh.wanandroidkotlin.fragment.TXFragment
import com.jbh.wanandroidkotlin.fragment.UserCenterFragment
import com.jbh.wanandroidkotlin.fragment.WDFragment
import com.jbh.wanandroidkotlin.manager.AppActivityManager
import com.jbh.wanandroidkotlin.manager.UserInfoManager
import kotlinx.android.synthetic.main.main_content_layout.*


class MainActivity :  BaseActivity() {




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initMainBottomTab()
        showFragment(0)
    }



    var indexFragment :IndexFragment ?= null
    var txFragment :TXFragment ?= null
    var wdFragment :WDFragment ?= null
    var userCenterFragment :UserCenterFragment ?= null



    /**
     * 展示页面
     */
    private fun showFragment(i: Int) {
        hindFragment()
        supportFragmentManager.beginTransaction()
            .apply {
                var fragment:Fragment ?= null
                when(i){
                    0 -> {
                        if (indexFragment==null) {
                            indexFragment = IndexFragment.newInstance()
                            add(R.id.content, indexFragment!!)
                        }
                        fragment = indexFragment!!
                    }

                    1 -> {
                        if (txFragment==null) {
                            txFragment = TXFragment.newInstance()
                            add(R.id.content, txFragment!!)
                        }
                        fragment = txFragment!!
                    }


                    2 -> {
                        if (wdFragment==null) {
                            wdFragment = WDFragment.newInstance()
                            add(R.id.content, wdFragment!!)
                        }
                        fragment = wdFragment!!
                    }



                    3 -> {
                        if (userCenterFragment==null) {
                            userCenterFragment = UserCenterFragment.newInstance()
                            add(R.id.content, userCenterFragment!!)
                        }
                        fragment = userCenterFragment!!
                    }
                }
                show(fragment!!)
        }.commit()
    }



    private fun hindFragment() {
        supportFragmentManager.beginTransaction()
            .apply {
                if (indexFragment!=null){
                    hide(indexFragment!!)
                }
                if (txFragment!=null){
                    hide(txFragment!!)
                }
                if (wdFragment!=null){
                    hide(wdFragment!!)
                }
                if (userCenterFragment!=null){
                    hide(userCenterFragment!!)
                }
            commit()
        }
    }







    /***
     * 底部tab
     */
    private fun initMainBottomTab() {
        mainTabLayout.addTab(getTab("首页",R.mipmap.icon_home_press))
        mainTabLayout.addTab(getTab("体系",R.mipmap.icom_bf))
        mainTabLayout.addTab(getTab("问答",R.mipmap.icon_c))
        mainTabLayout.addTab(getTab("我的",R.mipmap.icon_me))
        mainTabLayout.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                showFragment(tab!!.position)
                when(tab!!.position){
                    0 ->{tab.icon = resources.getDrawable(R.mipmap.icon_home_press)}
                    1 ->{tab.icon = resources.getDrawable(R.mipmap.icom_bf_press)}
                    2 ->{tab.icon = resources.getDrawable(R.mipmap.icon_c_pres)}
                    3 ->{tab.icon = resources.getDrawable(R.mipmap.icon_me_pres)}
                }
            }
            override fun onTabUnselected(tab: TabLayout.Tab?) {
                when(tab!!.position){
                    0 ->{tab.icon = resources.getDrawable(R.mipmap.icon_home)}
                    1 ->{tab.icon = resources.getDrawable(R.mipmap.icom_bf)}
                    2 ->{tab.icon = resources.getDrawable(R.mipmap.icon_c)}
                    3 ->{tab.icon = resources.getDrawable(R.mipmap.icon_me)}
                }
            }
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })
    }


    /**
     */
    private fun getTab(tabText : String,icon:Int): TabLayout.Tab {
        return mainTabLayout.newTab()
            .apply {
                text = tabText
                setIcon(icon)
            }
    }




}


